<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.tensordot &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Linear algebra (numpy.linalg)" href="../routines.linalg.html" >
    <link rel="next" title="numpy.einsum" href="numpy.einsum.html" >
    <link rel="prev" title="numpy.matmul" href="numpy.matmul.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.linalg.html" accesskey="U">Linear algebra (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.linalg</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.einsum.html" title="numpy.einsum"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.matmul.html" title="numpy.matmul"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.matmul.html"
                        title="previous chapter">numpy.matmul</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.einsum.html"
                        title="next chapter">numpy.einsum</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-tensordot">
<h1>numpy.tensordot<a class="headerlink" href="#numpy-tensordot" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.tensordot">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">tensordot</code><span class="sig-paren">(</span><em class="sig-param">a</em>, <em class="sig-param">b</em>, <em class="sig-param">axes=2</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/core/numeric.py#L913-L1103"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.tensordot" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute tensor dot product along specified axes.</p>
<p>Given two tensors, <em class="xref py py-obj">a</em> and <em class="xref py py-obj">b</em>, and an array_like object containing
two array_like objects, <code class="docutils literal notranslate"><span class="pre">(a_axes,</span> <span class="pre">b_axes)</span></code>, sum the products of
<em class="xref py py-obj">a</em>’s and <em class="xref py py-obj">b</em>’s elements (components) over the axes specified by
<code class="docutils literal notranslate"><span class="pre">a_axes</span></code> and <code class="docutils literal notranslate"><span class="pre">b_axes</span></code>. The third argument can be a single non-negative
integer_like scalar, <code class="docutils literal notranslate"><span class="pre">N</span></code>; if it is such, then the last <code class="docutils literal notranslate"><span class="pre">N</span></code> dimensions
of <em class="xref py py-obj">a</em> and the first <code class="docutils literal notranslate"><span class="pre">N</span></code> dimensions of <em class="xref py py-obj">b</em> are summed over.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>a, b</strong><span class="classifier">array_like</span></dt><dd><p>Tensors to “dot”.</p>
</dd>
<dt><strong>axes</strong><span class="classifier">int or (2,) array_like</span></dt><dd><ul class="simple">
<li><p>integer_like
If an int N, sum over the last N axes of <em class="xref py py-obj">a</em> and the first N axes
of <em class="xref py py-obj">b</em> in order. The sizes of the corresponding axes must match.</p></li>
<li><p>(2,) array_like
Or, a list of axes to be summed over, first sequence applying to <em class="xref py py-obj">a</em>,
second to <em class="xref py py-obj">b</em>. Both elements array_like must be of the same length.</p></li>
</ul>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>output</strong><span class="classifier">ndarray</span></dt><dd><p>The tensor dot product of the input.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="numpy.dot.html#numpy.dot" title="numpy.dot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dot</span></code></a>, <a class="reference internal" href="numpy.einsum.html#numpy.einsum" title="numpy.einsum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">einsum</span></code></a></p>
</div>
<p class="rubric">Notes</p>
<dl class="simple">
<dt>Three common use cases are:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">axes</span> <span class="pre">=</span> <span class="pre">0</span></code> : tensor product <img class="math" src="../../_images/math/232d16915a6a881859008fd31aa4030c77404fbe.svg" alt="a\otimes b"/></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">axes</span> <span class="pre">=</span> <span class="pre">1</span></code> : tensor dot product <img class="math" src="../../_images/math/54f6d1f29b7b82da3495240f1b237c3e66eea92c.svg" alt="a\cdot b"/></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">axes</span> <span class="pre">=</span> <span class="pre">2</span></code> : (default) tensor double contraction <img class="math" src="../../_images/math/cd7dd9aecd7a82fbc255a62366b6145d26c926ca.svg" alt="a:b"/></p></li>
</ul>
</dd>
</dl>
<p>When <em class="xref py py-obj">axes</em> is integer_like, the sequence for evaluation will be: first
the -Nth axis in <em class="xref py py-obj">a</em> and 0th axis in <em class="xref py py-obj">b</em>, and the -1th axis in <em class="xref py py-obj">a</em> and
Nth axis in <em class="xref py py-obj">b</em> last.</p>
<p>When there is more than one axis to sum over - and they are not the last
(first) axes of <em class="xref py py-obj">a</em> (<em class="xref py py-obj">b</em>) - the argument <em class="xref py py-obj">axes</em> should consist of
two sequences of the same length, with the first axis to sum over given
first in both sequences, the second axis second, and so forth.</p>
<p>The shape of the result consists of the non-contracted axes of the
first tensor, followed by the non-contracted axes of the second.</p>
<p class="rubric">Examples</p>
<p>A “traditional” example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">60.</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">24.</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">],[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(5, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">array([[4400., 4730.],</span>
<span class="go">       [4532., 4874.],</span>
<span class="go">       [4664., 5018.],</span>
<span class="go">       [4796., 5162.],</span>
<span class="go">       [4928., 5306.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># A slower but equivalent way of computing the same...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">5</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
<span class="gp">... </span>  <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
<span class="gp">... </span>      <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">b</span><span class="p">[</span><span class="n">n</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">j</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">==</span> <span class="n">d</span>
<span class="go">array([[ True,  True],</span>
<span class="go">       [ True,  True],</span>
<span class="go">       [ True,  True],</span>
<span class="go">       [ True,  True],</span>
<span class="go">       [ True,  True]])</span>
</pre></div>
</div>
<p>An extended example taking advantage of the overloading of + and *:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">;</span> <span class="n">A</span>
<span class="go">array([[[1, 2],</span>
<span class="go">        [3, 4]],</span>
<span class="go">       [[5, 6],</span>
<span class="go">        [7, 8]]])</span>
<span class="go">array([[&#39;a&#39;, &#39;b&#39;],</span>
<span class="go">       [&#39;c&#39;, &#39;d&#39;]], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">)</span> <span class="c1"># third argument default is 2 for double-contraction</span>
<span class="go">array([&#39;abbcccdddd&#39;, &#39;aaaaabbbbbbcccccccdddddddd&#39;], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">array([[[&#39;acc&#39;, &#39;bdd&#39;],</span>
<span class="go">        [&#39;aaacccc&#39;, &#39;bbbdddd&#39;]],</span>
<span class="go">       [[&#39;aaaaacccccc&#39;, &#39;bbbbbdddddd&#39;],</span>
<span class="go">        [&#39;aaaaaaacccccccc&#39;, &#39;bbbbbbbdddddddd&#39;]]], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="c1"># tensor product (result too long to incl.)</span>
<span class="go">array([[[[[&#39;a&#39;, &#39;b&#39;],</span>
<span class="go">          [&#39;c&#39;, &#39;d&#39;]],</span>
<span class="go">          ...</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">array([[[&#39;abbbbb&#39;, &#39;cddddd&#39;],</span>
<span class="go">        [&#39;aabbbbbb&#39;, &#39;ccdddddd&#39;]],</span>
<span class="go">       [[&#39;aaabbbbbbb&#39;, &#39;cccddddddd&#39;],</span>
<span class="go">        [&#39;aaaabbbbbbbb&#39;, &#39;ccccdddddddd&#39;]]], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">array([[[&#39;abb&#39;, &#39;cdd&#39;],</span>
<span class="go">        [&#39;aaabbbb&#39;, &#39;cccdddd&#39;]],</span>
<span class="go">       [[&#39;aaaaabbbbbb&#39;, &#39;cccccdddddd&#39;],</span>
<span class="go">        [&#39;aaaaaaabbbbbbbb&#39;, &#39;cccccccdddddddd&#39;]]], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
<span class="go">array([&#39;abbbcccccddddddd&#39;, &#39;aabbbbccccccdddddddd&#39;], dtype=object)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">tensordot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)))</span>
<span class="go">array([&#39;acccbbdddd&#39;, &#39;aaaaacccccccbbbbbbdddddddd&#39;], dtype=object)</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>